package com.learn.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;

/**
 * @author wangxing
 * @version 2020/7/30 11:43 Administrator
 */
public class AioClient {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1",9090)).get();
        socketChannel.write(ByteBuffer.wrap("hello server i am AioClient".getBytes()));
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        Integer len = socketChannel.read(buffer).get();
        if(len!=-1){
            System.out.println("收到服务端消息 "+ new String(buffer.array()));
        }

    }

}
